package org.tgcloud.group.core.ExportUtils;

import com.alibaba.excel.EasyExcel;
import org.tgcloud.group.core.RowHeightHandler;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

public class ExcelExportUtil {

    /**
     * 导出 Excel 文件（Http 响应方式）
     * @param response HttpServletResponse
     * @param fileName 导出文件名（不含后缀）
     * @param headList 表头（List<List<String>>，每一列可能多级标题）
     * @param dataList 数据内容（List<Map<String,Object>> 或 List<VO>）
     */
    public static void export(HttpServletResponse response,
                              String fileName,
                              List<List<String>> headList,
                              List<Map<String, Object>> dataList) throws IOException {
        // 设置响应
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String encodedFileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName + ".xlsx");

        // 写入
        EasyExcel.write(response.getOutputStream())
                .head(headList)
                .sheet("Sheet1")
                .doWrite(dataList);
    }

    /**
     * 快捷方法：VO 模式导出（如果你用的是 Java bean）
     * @param response
     * @param fileName
     * @param clazz VO 类
     * @param dataList VO 列表
     */
    public static <T> void export(HttpServletResponse response,
                                  String fileName,
                                  Class<T> clazz,
                                  List<T> dataList) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String encodedFileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName + ".xlsx");

        EasyExcel.write(response.getOutputStream(), clazz)
                .sheet("Sheet1")
                .doWrite(dataList);
    }

    public static void exportObject(HttpServletResponse response,
                              String fileName,
                              List<List<String>> headList,
                              List<List<Object>> dataRows) throws IOException {
        // 设置响应
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String encodedFileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName + ".xlsx");

        // 写入
        EasyExcel.write(response.getOutputStream())
                .head(headList)
//                .registerWriteHandler(new AutoWidthHandler(headList, dataRows))   // 宽度自适应
                .registerWriteHandler(new RowHeightHandler())                     // 行高控制
                .sheet("Sheet1")
                .doWrite(dataRows);

    }

}
